通用颜色对话框 – Windows API

最近更新于 2024-05-05 14:18

环境

Windows SDK 10.0(Windows 11)
平台工具集:Visual Studio 2022 v143
C 语言标准:C17
字符集:Unicode

示例

基本使用示例

#include <Windows.h>

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPreInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
    CHOOSECOLOR cc = {0}; // 颜色选择对话框结构 
    static COLORREF acrCustClr[16]; // 16 个自定义颜色

    cc.lStructSize = sizeof(cc);
    cc.hwndOwner = NULL; // 对话框窗口的父窗口
    cc.lpCustColors = (LPDWORD) acrCustClr; // 传入自定义颜色
    cc.rgbResult = RGB(120, 130, 140); // 初始颜色
    cc.Flags = CC_FULLOPEN | CC_RGBINIT; // 显示“规定自定义颜色”的内容 | 用 rgbResult 的颜色值初始化

    WCHAR s[32]; // 用于存放颜色的打印输出信息
    ZeroMemory(s, sizeof(s));

    AllocConsole(); // 申请控制台窗口用于显示信息
    HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出句柄

    if (ChooseColor(&cc)) // 打印输出选择的颜色
    {
        wsprintf(s, L"R:%d,G:%d,B:%d\n", GetRValue(cc.rgbResult), GetGValue(cc.rgbResult), GetBValue(cc.rgbResult));
        WriteConsole(out, s, wcslen(s), NULL, NULL);
    }

    system("pause");
}

可以通过给 acrCustClr 赋值,设置 16 自定义颜色,我这里没设置默认初始化为 0 了,就是黑色
file

也可以选择颜色或者设置颜色值,单击添加到自定义颜色,会在自定义颜色处显示,并将颜色值存放到 acrCustClr 中
file

点确定后会将选择的颜色存放到 rgbResult 中,这里通过控制台打印输出了
file

应用示例

#include <Windows.h>

#define ID_PEN_SET          100
#define ID_BRUSH_SET        101

#define PEN_SIZE            5 // 画笔宽度

COLORREF g_crPen = RGB(255, 0, 0); // 画笔颜色
COLORREF g_crBrush = RGB(0, 0, 255); // 画刷颜色

LRESULT CALLBACK wndProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
{
    static HPEN pen;
    static HBRUSH brush;
    static CHOOSECOLOR cc;
    static COLORREF acrCustClr[16];

    switch (msg)
    {
        case WM_CREATE:
        {
            // 颜色设置按钮
            CreateWindowEx(0, L"BUTTON", L"设置画笔颜色", WS_CHILD | WS_VISIBLE, 20, 20, 120, 20, wnd, (HMENU)ID_PEN_SET, (HINSTANCE)GetWindowLongPtr(wnd, GWLP_HINSTANCE), NULL);
            CreateWindowEx(0, L"BUTTON", L"设置画刷颜色", WS_CHILD | WS_VISIBLE, 200, 20, 120, 20, wnd, (HMENU)ID_BRUSH_SET, (HINSTANCE)GetWindowLongPtr(wnd, GWLP_HINSTANCE), NULL);
            // 颜色选择框结构体初始化
            cc.lStructSize = sizeof(cc);
            cc.hwndOwner = wnd;
            cc.lpCustColors = acrCustClr;
            cc.Flags = CC_FULLOPEN | CC_RGBINIT;
            // 初始创建画笔画刷
            pen = CreatePen(PS_SOLID, PEN_SIZE, g_crPen);
            brush = CreateSolidBrush(g_crBrush);
            break;
        }
        case WM_PAINT:
        {
            static PAINTSTRUCT ps;
            static HDC hdc;

            hdc = BeginPaint(wnd, &ps); // 打开绘图上下文
            HPEN oldPen = SelectObject(hdc, pen); // 替换画笔
            HBRUSH oldBrush = SelectObject(hdc, brush); // 替换画刷
            Rectangle(hdc, 50, 200, 300, 300); // 绘制矩形
            SelectObject(hdc, oldPen); // 换回画笔
            SelectObject(hdc, oldBrush); // 换回画刷
            EndPaint(wnd, &ps); // 关闭绘图上下文
            break;
        }
        case WM_COMMAND:
        {
            switch (LOWORD(wp))
            {
                case ID_PEN_SET:
                {
                    if (HIWORD(wp) == BN_CLICKED)
                    {
                        cc.rgbResult = g_crPen;
                        if (ChooseColor(&cc))
                        {
                            g_crPen = cc.rgbResult;
                        }
                        DeleteObject(pen); // 删除原自定义画笔
                        pen = CreatePen(PS_SOLID, PEN_SIZE, g_crPen); // 创建新画笔
                    }
                    break;
                }
                case ID_BRUSH_SET:
                {
                    if (HIWORD(wp) == BN_CLICKED)
                    {
                        cc.rgbResult = g_crBrush;
                        if (ChooseColor(&cc))
                        {
                            g_crBrush = cc.rgbResult;
                        }
                        DeleteObject(brush); // 删除原画刷
                        brush = CreateSolidBrush(g_crBrush); // 创建新画刷
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            InvalidateRgn(wnd, 0, TRUE);
            break;
        }
        case WM_CLOSE:
        {
            DestroyWindow(wnd);
            break;
        }
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
        default:
        {
            return DefWindowProc(wnd, msg, wp, lp);
        }
    }
    return 0;
}

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPreInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
    PCWSTR className = L"desktopAPP";
    WNDCLASSEX wc = {0};
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.lpszClassName = className;
    wc.lpfnWndProc = wndProc;
    if (!RegisterClassEx(&wc))
    {
        return 100;
    }

    HWND wnd = CreateWindowEx(0, className, L"通用颜色对话框使用", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 380, 400, NULL, NULL, hInstance, NULL);
    if (!wnd)
    {
        return 101;
    }

    ShowWindow(wnd, nCmdShow);
    UpdateWindow(wnd);

    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

file

通用颜色对话框 – Windows API
Scroll to top